package com.acwing.partition11;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2021/12/22 15:16
 */
public class AC1073树的中心 {

    private static final int N = 10010;
    private static int[] h = new int[N], e = new int[N << 1], ne = new int[N << 1], w = new int[N << 1];
    //d1[i]、d2[i]分别存储了从i向下的最长路径和次长路径；up[i]用于存储节点i向上的最长路径；p[i]表示在求向下的最长时，i节点经过的节点
    private static int[] d1 = new int[N], d2 = new int[N], up = new int[N], p = new int[N];
    private static int n, idx = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = Integer.parseInt(reader.readLine());
        Arrays.fill(h, -1);
        for (int i = 0; i < n - 1; i++) {
            String[] ss = reader.readLine().split("\\s+");
            int a = Integer.parseInt(ss[0]), b = Integer.parseInt(ss[1]), c = Integer.parseInt(ss[2]);
            add(a, b, c);
            add(b, a, c);
        }
        dfs1(1, -1);
        dfs2(1, -1);
        int ans = d1[1];
        for (int i = 2; i <= n; i++) ans = Math.min(ans, Math.max(d1[i], up[i]));
        writer.write(ans + "\n");
        writer.flush();
    }

    private static void dfs2(int u, int parent) {
        for (int i = h[u]; i != -1; i = ne[i]) {
            int v = e[i];
            if (v == parent) continue;
            //通过父节点来更新子节点
            if (p[u] == v) up[v] = Math.max(up[u], d2[u]) + w[i];
            else up[v] = Math.max(up[u], d1[u]) + w[i];
            dfs2(v, u);
        }
    }

    private static int dfs1(int u, int parent) {
        d1[u] = d2[u] = 0;
        for (int i = h[u]; i != -1; i = ne[i]) {
            int v = e[i];
            if (parent == v) continue;
            int res = dfs1(v, u) + w[i];
            if (res >= d1[u]) {
                d2[u] = d1[u];
                d1[u] = res;
                p[u] = v;
            } else if (res > d2[u]) {
                d2[u] = res;
            }
        }
        return d1[u];
    }

    private static void add(int a, int b, int c) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx;
        w[idx++] = c;
    }
}
